package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.*;
import com.ygqh.baby.mapper.YgUserCultivationDayMapper;
import com.ygqh.baby.model.YgUserCultivationDayModel;
import com.ygqh.baby.model.YgUserCultivationOrderPrice;
import com.ygqh.baby.po.*;
import com.ygqh.baby.service.YgAgentInviteRecordService;
import com.ygqh.baby.service.YgAgentService;
import com.ygqh.baby.service.YgUserCultivationDayService;
import com.ygqh.baby.utils.DateConvertUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class YgUserCultivationDayServiceImpl implements YgUserCultivationDayService {

	@Autowired
	private YgUserCultivationDayMapper ygUserCultivationDayMapper;
	@Autowired
	private YgAgentInviteRecordService ygAgentInviteRecordService;
	@Autowired
	private YgAgentService ygAgentService;

	@Override
	public List<YgUserCultivationDay> find() {
		YgUserCultivationDayExample example = new YgUserCultivationDayExample();
		return ygUserCultivationDayMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgUserCultivationDayExample example = new YgUserCultivationDayExample();
		return ygUserCultivationDayMapper.countByExample(example);
	}

	@Override
	public YgUserCultivationDay findById(Long id) {
		return ygUserCultivationDayMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgUserCultivationDay ygUserCultivationDay) {
		return ygUserCultivationDayMapper.insertSelective(ygUserCultivationDay);
	}

	@Override
	public int update(YgUserCultivationDay ygUserCultivationDay) {
		return ygUserCultivationDayMapper.updateByPrimaryKeySelective(ygUserCultivationDay);
	}

	@Override
	public ResultSet<YgUserCultivationDay> search(QueryInfo queryInfo, String q) {
		List<YgUserCultivationDay> list = ygUserCultivationDayMapper.selectSuper(queryInfo, q);
		int count = ygUserCultivationDayMapper.countSuper(q);
		ResultSet<YgUserCultivationDay> resultSet = new ResultSet<YgUserCultivationDay>(count, list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgUserCultivationDayExample example = new YgUserCultivationDayExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgUserCultivationDay record = new YgUserCultivationDay();

		return ygUserCultivationDayMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[] { id });
	}

	@Override
	public int saveOrUpdate(YgUserCultivationDay ygUserCultivationDay) {
		int r = 0;
		if (ygUserCultivationDay.getId() != null) {
			r = this.update(ygUserCultivationDay);
		} else {
			r = this.save(ygUserCultivationDay);
		}
		return r;
	}

	@Override
	public List<YgUser> queryAffiliateUser() {
		return ygUserCultivationDayMapper.selectAffiliateUser();
	}

	@Override
	public List<YgUserCultivationOrderPrice> findOrderPriceByDay(String yesterday) {

		return ygUserCultivationDayMapper.selectOrderPriceByDay(yesterday);
	}

	@Override
	public List<YgUserCultivationOrderPrice> findReturnPriceByDay(String yesterday) {

		return ygUserCultivationDayMapper.selectReturnPriceByDay(yesterday);
	}

	@Override
	public List<YgUserCultivationDay> findUserCultivationByUserId(Long userId) {

		return ygUserCultivationDayMapper.selectUserCultivationByUserId(userId);
	}

	@Override
	public List<YgUserCultivationDayModel> queryUserCultivationGroupByUserId(Long userId) {
		YgAgentInviteRecord ygAgent = ygAgentInviteRecordService.findValidByReceiveUserId(userId);
		Date startDate = null;
		if (ygAgent != null) {
			startDate = DateConvertUtils.formatToDate(ygAgent.getReceiveTime(), "yyyy-MM-dd");
		}
		return ygUserCultivationDayMapper.selectUserCultivationGroupByUserId(userId, startDate);
	}

	@Override
	public ResultSet<YgUserCultivationDayModel> findUserCultivationSumGroupByMonth(Long userId) {
		YgAgent ygAgent = ygAgentService.findValidByUserId(userId, UserType.UserAgent);
		Date startDate = null;
		if (ygAgent != null) {
			startDate = DateConvertUtils.formatToDate(ygAgent.getCheckTime(), "yyyy-MM-dd");
		}
		List<YgUserCultivationDayModel> list = ygUserCultivationDayMapper.selectUserCultivationGroupByUserId(userId, startDate);
		return new ResultSet<>(0, list);
	}

	@Override
	public List<YgUserCultivationDay> affiliateUserReportV2(Date startDate, Date endDate, Long userId) {
		startDate = DateConvertUtils.getDateStart(startDate);
		endDate = DateConvertUtils.getDateStart(endDate);
		return ygUserCultivationDayMapper.affiliateUserReportV2(startDate, endDate, userId);
	}

	@Override
	public int addBatch(List<YgUserCultivationDay> list) {
		if (CollectionUtils.isEmpty(list)) {
			return 0;
		}

		return ygUserCultivationDayMapper.addBatch(list);
	}

	@Override
	public List<YgUserCultivationDay> findByDay(Date startDate, Date endDate) {
		startDate = DateConvertUtils.getDateStart(startDate);
		endDate = DateConvertUtils.getDateStart(endDate);
		YgUserCultivationDayExample example = new YgUserCultivationDayExample();
		example.createCriteria().andDayBetween(startDate, endDate);
		return ygUserCultivationDayMapper.selectByExample(example);
	}

	@Override
	public BigDecimal findUserCultivationSumByUserId(Long userId) {
		return ygUserCultivationDayMapper.selectUserCultivationSumByUserId(userId, null, null);
	}

	@Override
	public BigDecimal getUserShareOrderPrice(YgUser ygUser, Date startDate, Date endDate) {
		BigDecimal orderPrice = ygUserCultivationDayMapper.selectUserCultivationSumByUserId(ygUser.getId(), startDate, endDate);
		return orderPrice == null ? new BigDecimal("0.00") : orderPrice;
	}

	@Override
	public int additional(List<YgUserCultivationDay> otherDayCultivation) {
		if (CollectionUtils.isEmpty(otherDayCultivation)) {
			return 0;
		}
		return ygUserCultivationDayMapper.additional(otherDayCultivation);
	}

	@Override
	public List<YgUserCultivationDay> getSumGroupByUserId(Date start, Date end) {
		return ygUserCultivationDayMapper.getSumGroupByUserId(start, end);
	}

	@Override
	public List<YgUserCultivationDay> getSumGroupByMonth(Date start, Date end, Long userId) {
		return ygUserCultivationDayMapper.getSumGroupByMonth(start, end, userId);
	}

	@Override
	public List<YgUserCultivationDay> findByUserIdAndDay(Date startDate, Date endDate, Long userId) {
		YgAgentInviteRecord record = ygAgentInviteRecordService.findValidByReceiveUserId(userId);
		if (record.getReceiveTime().after(startDate)) {
			startDate = record.getReceiveTime();
		}
		YgUserCultivationDayExample example = new YgUserCultivationDayExample();
		example.createCriteria().andDayBetween(startDate, endDate).andUserIdEqualTo(userId)
			.andStatusEqualTo(DataStatus.Valid.name()).andTotalPriceGreaterThan(new BigDecimal("0.00"));
		example.setOrderByClause("day DESC");
		return ygUserCultivationDayMapper.selectByExample(example);
	}

}
